#include<stdio.h>
#include<stdlib.h>

typedef struct ENode{
	int 		 number;
	int 		 cipher;
	struct ENode 	*next;
}ENode;

int initiate(ENode **list){
	if(((*list) = (ENode*) malloc(sizeof(ENode))) == NULL){
		printf("space malloc failed\n");
		return 0;
	}

	(*list)->number  = 0;
	(*list)->cipher = 0;
	(*list)->next 	 = NULL;
	return 1;
}

int create(ENode **list, int number, int solid){
	int m = 0;
	int i = 0;
	ENode *head = NULL;
	ENode *p = NULL;	
	ENode *new = NULL;

	if(number <= 0){
		printf("The number required is illegal\n");
		return 0;
	}

	if(!initiate(list)){
		printf("initiate failed\n");
		return 0;
	}
	
	if(solid == 0){
		printf("Enter the Secret NUM:");
		scanf("%d", &m);
	}

	head = *list;
	p = head;
	for(i = 1; i <= number; i++){
		new = (ENode*) malloc(sizeof(ENode));
		if(new == NULL){
			printf("space malloc failed\n");
			return 0;
		}
		new->number = i;
		if(solid != 0){
			printf("Enter the cipher for %d:", i);
			scanf("%d", &m);
		}
		new->cipher = m;
		
		if(i == 1){
			p->next = new;
			new->next = new;
		}else{
			new->next = p->next;
			p->next = new;
		}
		p = p->next;
	}
	return 1;
}

int clear(ENode *head){
	ENode *p = head->next;
	ENode *t = head;
	int m = p->cipher;

	while(p->next != p){
		while(m != 1){
			t = p;
			p = p->next;
			--m;
		}
		t->next = p->next;
		printf("%d  ", p->number);
		m = p->cipher;
		free(p);
		p = t->next;
	}
	printf("%d\n", p->number);
	free(p);
	free(head);
}

int print(ENode *head){
	ENode *p = head;
	p = p->next;
	printf("%d:   %d\n",p->number, p->cipher);
	p = p->next;
	while(p != head->next){
		printf("%d:   %d\n", p->number, p->cipher);
		p = p->next;
	}
	return 1;
}

int main(){
	ENode *head;
	
	if(!create(&head, 10, 0)){
		printf("create cipher failed\n");
		return 0;
	}

	print(head);

	clear(head);
	
}
